﻿using System;
using System.Data.SqlClient;

namespace BwanaBank.BankOnline.WinUI.Window.Customer
{
    public partial class E_BlueCard : System.Web.UI.Page
    {
        private int amount;
        private string idAccount;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["customer"] == null && Session["customer"] == null)
            {
                Response.Redirect("~/");
            }
            begin2.Visible = false;
            var cn = new SqlConnection(@"Data Source=PAR-ROBHED-001\SQLEXPRESS;Initial Catalog=BWANABANk;Integrated Security=True");
            cn.Open();
            SqlCommand cmd = null;
            if (Request.Cookies["customer"] != null)
            {
                cmd = new SqlCommand("SELECT * FROM [User] WHERE customerNUm='" + Request.Cookies["customer"].Value + "'", cn);
            }
            else if (Session["customer"] != null)
            {
                cmd = new SqlCommand("SELECT * FROM [User] WHERE customerNum='" + Session["customer"] + "'", cn);
            }
            var dtr = cmd.ExecuteReader();
            if (dtr.Read())
            {
                var idUser = (int)dtr["idUser"];
                dtr.Dispose();
                dtr.Close();
                cmd = new SqlCommand("SELECT * FROM [Account] WHERE idUser='" + idUser + "'", cn);
                dtr = cmd.ExecuteReader();
                if (dtr.Read())
                {
                    idAccount = (string)dtr["idAccount"];
                    amount = (int)dtr["realBalance"];
                    LabelECB.Text = "Vous avez <b>" + amount + "</b>€ disponible sur votre compte n°<b>" + idAccount + "</b>.";
                }
                dtr.Dispose();
                dtr.Close();
            }
            cn.Close();
        }
        protected void Button_ECB(object sender, EventArgs e)
        {
            begin.Visible = false;
            begin2.Visible = true;
            LoginButton.Visible = false;

            Label2.Text = "Votre compte n°<b>" + idAccount + "</b> a bien été prélevé de la somme de <b>" + UserName.Text + "</b>€ afin de créditer votre nouvelle carte bleue virtuelle dont vous pouvez retrouver les informations ci-dessous :";

            var num1 = RandomNumber(1800, 2000);
            var num2 = RandomNumber(3600, 3800);
            int num3 = RandomNumber(5400, 5600);
            int num4 = RandomNumber(7200, 7400);
            var num_cb = num1.ToString() + "-" + num2.ToString() + "-" + num3.ToString() + "-" + num4.ToString();
            Label3.Text = num_cb;

            Label4.Text = Password.Text;

            int cryptogramme = RandomNumber(100, 999);

            int new_amount = amount - Convert.ToInt32(UserName.Text);

            Label5.Text = cryptogramme.ToString();

            var cn = new SqlConnection(@"Data Source=PAR-ROBHED-001\SQLEXPRESS;Initial Catalog=BWANABANK;Integrated Security=True");
            cn.Open();

            var cmd = new SqlCommand("insert into [E_CB] ([idEcb],[cryptogram],[DateEnd],[amount],[idAccount]) values ('" + num_cb + "','" + cryptogramme + "','" + Password.Text + "','" + UserName.Text + "','" + idAccount + "')", cn);
            cmd.ExecuteNonQuery();

            var cmd2 = new SqlCommand("UPDATE [Account] SET realBalance='" + new_amount + "' WHERE idAccount='" + idAccount + "'", cn);
            cmd2.ExecuteNonQuery();

            var cmd3 = new SqlCommand("insert into [Operation] ([description],[date],[amount],[type],[idAccount]) values ('ECB','" + DateTime.Now + "','" + UserName.Text + "','Débit','" + idAccount + "')", cn);
            cmd3.ExecuteNonQuery();

            cn.Close();
        }

        private int RandomNumber(int min, int max)
        {
            var random = new Random();
            return random.Next(min, max);
        }
    }
}
